package study.lxy.spring.springboot2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.lxy.spring.springboot2.entity.LoginUser;
import study.lxy.spring.springboot2.entity.MyResponseEntity;
import study.lxy.spring.springboot2.service.LoginService;

import javax.servlet.http.HttpSession;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping(value = "/login")
    public MyResponseEntity login(@ModelAttribute LoginUser user, HttpSession session){
        session.setAttribute("loginUser",user.getUserName());
        loginService.setSessionId(user);
        return MyResponseEntity.success("login success");
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public MyResponseEntity logout(HttpSession session){
        session.removeAttribute("loginUser");
        loginService.logOut();
        return MyResponseEntity.success("logout success");
    }
}
